package com.ajie.service.impl;


import com.ajie.config.security.service.UserDetailServiceImpl;
import com.ajie.entity.SysUser;
import com.ajie.mapper.SysMiniUserMapper;
import com.ajie.service.MiniService;
import com.ajie.service.SysUserService;
import com.ajie.utils.MD5Utils;
import com.ajie.utils.RandomString;
import com.ajie.utils.Result;
import com.ajie.utils.TokenUtil;
import com.ajie.vo.MiniUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class MiniServiceImpl implements MiniService {
    
   
    
    @Autowired
    private UserDetailServiceImpl detailServiceImpl;
    
    
    @Autowired
    private SysMiniUserMapper miniUserMapper;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Autowired
    private TokenUtil tokenUtil;
    
    
    @Value("${jwt.tokenHead}")
    private  String tokenHead;
    
    @Autowired
    private SysUserService userService;
    
    @Override
    public Result updateByOpenId(SysUser user) {
        log.info("该文件名是：MiniServiceImpl,该方法是：updateByOpenId");
        user.setPassword(passwordEncoder.encode(MD5Utils.md5(user.getPassword())));
        miniUserMapper.updateByOpenId(user);
        return Result.success("更新用户信息成功");
    }
    
    public  Result miniLogin(String openId){
        log.info("该文件名是：MiniServiceImpl,该方法是：miniLogin");
        UserDetails userDetails;
        userDetails = detailServiceImpl.loadUserByUsername(openId);
        log.info("user=>{}",userDetails);
        if (!userDetails.isEnabled()){
            return Result.fail("该账号已禁用,请联系管理员！");
        }
        log.info("微信小程序登录成功！在security对象中存入登陆者信息");
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        log.info("根据登录信息获取token");
        //需要借助jwt来生成token
        String token = tokenUtil.generateToken(userDetails);
        Map<String,Object> map = new HashMap<>(2);
        map.put("tokenHead",tokenHead);
        map.put("token",token);
        map.put("userInfo",userDetails);
        map.put("openid",openId);
        return Result.success("登录成功！",map);
    }
    
    @Override
    public Result miniRegister(String openId) {
        SysUser user = new SysUser();
        user.setUsername(RandomString.getCharAndNumr(6));
        user.setOpenId(openId);
        user.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/MY9lpHk6lv3LQ9YH1YK3nIzTSqkOVaT1vtKENo69hzm7Qq0jfY6ice1k7EUMnaecnIu0Q6Tbiav2YkqGsOTKdVgw/132");
        user.setNickName("微信用户");
        user.setPassword("123456");
        user.setStatus(true);
        userService.insert(user);
        return Result.success("注册成功");
    }
}
